<?php
/**
 * Created by PhpStorm.
 * User: ari
 * Date: 10/18/14
 * Time: 10:44 AM
 */
namespace BC\Games\Holdem\Hands;

use BC\Games\Holdem\Cards;
use BC\Games\Holdem\Deck;
use CPath\UnitTest\ITestable;
use CPath\UnitTest\IUnitTestRequest;

class NoHand implements IPokerHand, ITestable
{
	const RANK = 0;

	private $mCards;

	public function __construct(Array $cards) {
		sort($cards);
		$this->mCards = $cards;
	}

	function getName() {
		return "No Hand (High card)"; // TODO: ??
	}

	function getCards($withNullDelimiter=null) {
		return $this->mCards;
	}

	/**
	 * Get Hand rank
	 * @return int
	 */
	function getRank() {
		$rank = 0;
		$div = 1;
		foreach($this->mCards as $card)
			if($card)
				$rank += ($card & Cards::NUMBER_MASK) / $div *= 100;
		return $rank;
	}

	/**
	 * Compare two hands
	 * @param IPokerHand $Hand
	 * @return int 0, <0, or >0
	 */
	function compare(IPokerHand $Hand) {
		$rank = $this->getRank() - $Hand->getRank();

		return $rank;
	}

	/**
	 * Returns a string representation of this poker hand
	 * @return String
	 */
	function __toString() {
		$cards = $this->mCards;
		rsort($cards);
		$first = array_shift($cards);
		$cards = array_slice($cards, 0, 4);

		return
			"[" . Cards::printCards($first)
			. "], " . Cards::printCards($cards);
	}

	// Static

	/**
	 * Tests a poker hand to see if it matches an array of cards
	 * @param array $cards
	 * @param IPokerHand[] $Matches
	 * @throws \InvalidArgumentException
	 * @return bool true if the set of cards matches this poker hand
	 */
	static function match(Array $cards, &$Matches = array()) {
		PokerHands::getAllHands($cards, $Matches);

		return sizeof($Matches) === 0;
	}

	/**
	 * Perform a unit test
	 * @param IUnitTestRequest $Test the unit test request inst for this test session
	 * @return void
	 * @test --disable 0
	 * Note: Use doctag 'test' with '--disable 1' to have this ITestable class skipped during a build
	 */
	static function handleStaticUnitTest(IUnitTestRequest $Test) {
		$Deck = new Deck();
		$Deck->replay(Cards::ACE | Cards::HEARTS, 'player1');
		$Deck->replay(Cards::FIVE | Cards::CLUBS, 'player1');
		$Deck->replay(Cards::SIX | Cards::CLUBS, 'board');
		$Deck->replay(Cards::THREE | Cards::HEARTS, 'board');
		$Deck->replay(Cards::JACK | Cards::SPADES, 'board');
		$Deck->replay(Cards::NINE | Cards::CLUBS, 'board');
		$Deck->replay(Cards::EIGHT | Cards::DIAMONDS, 'board');

		$cards = $Deck->getDrawnCards('player1', 'board');
		if (!NoHand::match($cards, $Hands))
			$Test->fail("No-hand not matched to cards: " . Cards::printCards($cards));

		$Test->log("Matched No-hand to " . Cards::printCards($cards), $Test::VERBOSE);
	}
}